package com.project.finance.mobile.debit.controller;

import com.project.finance.common.mapper.TbVerifyMapper;
import com.project.finance.common.po.TbDebitOrder;
import com.project.finance.common.po.TbPaybackOrder;
import com.project.finance.common.po.TbVerify;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultCode;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.mobile.debit.service.DebitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 贺谋昌
 * @Date: 2019/12/10 10:34
 * @Description:
 */
@RestController
public class DebitControllerMobile {
    @Autowired
    @Qualifier(value = "DebitServiceImplM")
    DebitService debitService;

    @Autowired
    TbVerifyMapper mapper;

    /**
     * 上传身份证正面
     *
     * @param martipart
     * @param request
     * @return
     */
    @RequestMapping("/uploadIdFont")
    public Result uploadIdFont(MultipartFile martipart, HttpServletRequest request) {
        boolean isSuccess = debitService.uploadIdFont(martipart, request, "front");
        if (isSuccess) {
            return ResultGenerator.apiSuccess("上传成功");
        } else {
            return ResultGenerator.apiError("上传失败");
        }
    }

    /**
     * 上传身份证反面
     *
     * @return
     */
    @RequestMapping("/uploadIdBack")
    public Result uploadIdBack(MultipartFile martipart, HttpServletRequest request) {
        boolean isSuccess = debitService.uploadIdFont(martipart, request, "back");
        if (isSuccess) {
            return ResultGenerator.apiSuccess("上传成功");
        } else {
            return ResultGenerator.apiError("上传失败");
        }
    }

    /**
     * 上传审核信息
     *
     * @param verify
     * @return
     */
    @RequestMapping("/uploadVerify")
    public Result uploadVerify(@RequestBody TbVerify verify) {
        verify.setIdcardBack("https://farmimg-1251492284.cos.ap-chengdu.myqcloud.com/" + verify.getUserId() + "back");
        verify.setIdcardFront("https://farmimg-1251492284.cos.ap-chengdu.myqcloud.com/" + verify.getUserId() + "front");
        mapper.insert(verify);
        return ResultGenerator.apiSuccess("上传成功");
    }

    /**
     * 借款
     *
     * @return
     */
    @RequestMapping("/debit")
    public Result debit(@RequestBody TbDebitOrder debitOrder) {
        debitOrder.setDebitDate(new Date());
        debitService.insertDebit(debitOrder);
        return ResultGenerator.apiSuccess("申请成功");
    }

    /**
     * @param userId
     * @param paybackAmount
     * @param debitIdList
     * @return
     */
    @RequestMapping("/payback")
    public Result payback(@RequestParam("userId") Integer userId,
                          @RequestParam("paybackAmount") String paybackAmount,
                          @RequestParam("debitIdList") String debitIdList) {

        TbPaybackOrder paybackOrder = new TbPaybackOrder();
        paybackOrder.setUserId(userId);
        BigDecimal decimal = new BigDecimal(paybackAmount);
        paybackOrder.setPaybackAmount(decimal);
        debitService.insertPayback(paybackOrder);

        String[] debitArray = debitIdList.split(",");
        List<String> debitOrderIdList = Arrays.asList(debitArray);
        List<TbDebitOrder> debitOrderList = debitService.getTbDebitListByIds(debitOrderIdList);
        for (TbDebitOrder debitOrder: debitOrderList) {
            debitOrder.setSurplusAmount(new BigDecimal(0));
            debitOrder.setSurplusNumber(0);
        }
        debitService.updateDebitList(debitOrderList);
        return ResultGenerator.apiSuccess("请求成功");
    }
}
